package nc.bs.impl.yfk;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yonyou.nccloud.gateway.adapter.GateWayUtil;
import nc.bs.framework.common.NCLocator;
import nc.bs.itf.yfk.IYFKService;
import nc.itf.uap.cloud.ICloudService;
import nc.vo.pub.BusinessException;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yonyou.ucf.mdf.util.BIPUtil;

public class YFKServiceImpl implements IYFKService{

	@Override
	public JSONObject queryYFKBillDetailList(String baseURL,
			String queryBillListURL, String access_token, JSONObject bodyParam)
			throws BusinessException {
		JSONObject ret = new JSONObject();
        Map<String, String> acctokenmap = this.getQueryParam(access_token);
        String url = this.joinRequestParamURL(baseURL ,queryBillListURL);
        Map body = bodyParam.toJavaObject(Map.class);
        String resp = BIPUtil.getPostQueryParamAndBodyParam(url, acctokenmap, body);
        try{
            JSONObject resjson = JSON.parseObject(resp);
            if("200".equals(resjson.getString("code"))){
            	String yfkBillID = resjson.getJSONObject("data").getJSONArray("recordList").getJSONObject(0).getString("id");
            	ret.put("id", yfkBillID);
            	
            }else{
            	throw new BusinessException(resp);
            }
        }catch(Exception e){
        	throw new BusinessException("友费控 单据列表查询接口,响应信息【"+resp+"】,格式化异常:" + e.getMessage());
        }
		return ret;
	}

	@Override
	public JSONObject queryYFKBillDetailListByGateWay(String baseURL, String queryBillListURL, Map<String, String> header, Map<String, Object> bodyParam, String sysid) throws BusinessException {
		JSONObject ret = new JSONObject();
		ICloudService cloudService = NCLocator.getInstance().lookup(ICloudService.class);
//		String url = this.joinRequestParamURL(baseURL ,queryBillListURL);
		String resp = cloudService.callOpenPlatform(queryBillListURL, "POST", "json", header, bodyParam);
		try{
			JSONObject resjson = JSON.parseObject(resp);
			if("200".equals(resjson.getString("code"))){
				String yfkBillID = resjson.getJSONObject("data").getJSONArray("recordList").getJSONObject(0).getString("id");
				ret.put("id", yfkBillID);

			}else{
				throw new BusinessException(resp);
			}
		}catch(Exception e){
			throw new BusinessException("友费控 单据列表查询接口,响应信息【"+resp+"】,格式化异常:" + e.getMessage());
		}
		return ret;
	}

	@Override
	public List<JSONObject> queryYFKBillAttachList(String baseURL,
			String queryBillListURL, String access_token, JSONObject bodyParam)
			throws BusinessException {
		List<JSONObject> listatts = new ArrayList<JSONObject>();
        Map<String, String> acctokenmap = this.getQueryParam(access_token);
        String url = this.joinRequestParamURL(baseURL ,queryBillListURL);
        Map body = bodyParam.toJavaObject(Map.class);
		String resp = GateWayUtil.callRemoteGetUrlBySpecificGateway(url, "POST", "json", null, body, "ssc_baozhang");
//		String resp = BIPUtil.getPostQueryParamAndBodyParam(url, acctokenmap, body);
        try{
            JSONObject resjson = JSON.parseObject(resp);
            if("200".equals(resjson.getString("code"))){
            	JSONArray childrens = resjson.getJSONArray("data").getJSONObject(0).getJSONArray("children");
            	if(childrens.isEmpty()) return listatts;
            	for(int i = 0 ; i < childrens.size(); i++){
            		JSONObject item = childrens.getJSONObject(i); // 
            		JSONArray fpArrs = item.getJSONArray("children");
            		if(null != fpArrs){// 说明是发票
            			for(int j = 0; j < fpArrs.size(); j++){
            				 JSONObject fpObj = fpArrs.getJSONObject(j);
            				 JSONObject data = this.HandlerYFKAttach(fpObj);
            				 listatts.add(data);
            			}
            		}else{// 说明是附件
            			JSONObject data = this.HandlerYFKAttach(item);
            			listatts.add(data);
            		}
            	}
            	
            }else{
            	throw new BusinessException(resp);
            }
        }catch(Exception e){
        	throw new BusinessException("友费控 单据列表查询接口,响应信息【"+resp+"】,格式化异常:" + e.getMessage());
        }
		return listatts;
	}

	@Override
	public List<JSONObject> queryYFKBillAttachListByGateWay(String baseURL, String queryBillListURL, Map<String, String> header, Map<String, Object> bodyParam, String sysid) throws BusinessException {
		List<JSONObject> listatts = new ArrayList<JSONObject>();
		ICloudService cloudService = NCLocator.getInstance().lookup(ICloudService.class);
//		String url = this.joinRequestParamURL(baseURL ,queryBillListURL);
		String resp = cloudService.callOpenPlatform(queryBillListURL, "POST", "json", header, bodyParam);
		try{
			JSONObject resjson = JSON.parseObject(resp);
			if("200".equals(resjson.getString("code"))){
				JSONArray childrens = resjson.getJSONArray("data").getJSONObject(0).getJSONArray("children");
				if(childrens.isEmpty()) return listatts;
				for(int i = 0 ; i < childrens.size(); i++){
					JSONObject item = childrens.getJSONObject(i); //
					JSONArray fpArrs = item.getJSONArray("children");
					if(null != fpArrs){// 说明是发票
						for(int j = 0; j < fpArrs.size(); j++){
							JSONObject fpObj = fpArrs.getJSONObject(j);
							JSONObject data = this.HandlerYFKAttach(fpObj);
							listatts.add(data);
						}
					}else{// 说明是附件
						JSONObject data = this.HandlerYFKAttach(item);
						listatts.add(data);
					}
				}

			}else{
				throw new BusinessException(resp);
			}
		}catch(Exception e){
			throw new BusinessException("友费控 单据列表查询接口,响应信息【"+resp+"】,格式化异常:" + e.getMessage());
		}
		return listatts;
	}

	/**
	 * 处理友费控返回的 单据信息，提取主要信息 即附件地址处理
	 * @param json
	 * @return
	 * @throws BusinessException
	 */
	protected JSONObject HandlerYFKAttach(JSONObject json) throws BusinessException{
		JSONObject data = new JSONObject();
		String code = json.getString("code");
		int index = code.lastIndexOf(".");
		String attachmentName = code.substring(0, index);
		String attachmentType = code.substring(index+1);
		String attachmentPath = json.getJSONObject("properties").getString("downloadUrlForPreview");
		data.put("attachmentPath", attachmentPath);
		data.put("attachmentType", attachmentType);
		data.put("attachmentName", attachmentName);
		return data;
	}
	
	/**
	 * 设置地址query中的token
	 * @return
	 * @throws BusinessException
	 */
	protected Map<String, String> getQueryParam(String access_token) throws BusinessException {
        Map<String, String> acctokenmap = new HashMap<>();
        acctokenmap.put("access_token", access_token);
		return acctokenmap;
	}
	/**
	 * 拼接地址
	 * @param baseURL
	 * @param queryBillListURL
	 * @return
	 * @throws BusinessException
	 */
	protected String joinRequestParamURL(String baseURL, String queryBillListURL) throws BusinessException {
		return baseURL + queryBillListURL;
	}
	
}
